import { Image, Platform } from 'react-native';
import RNFetchBlob from 'react-native-blob-util';
import Share, { ShareOptions, ShareSingleOptions, Social } from 'react-native-share';
import Toast from 'react-native-toast-message';

import { APP_NAME, APP_STORE_URL, SHARE_MESSAGE } from '@/constants';

export class ShareSingleton {
  private static instance: ShareSingleton;
  private static icon: string = '';

  public static getInstance(): ShareSingleton {
    if (!ShareSingleton.instance) {
      ShareSingleton.instance = new ShareSingleton();
      const imageSource = require('@assets/logo.png');
      const imageUri = Image.resolveAssetSource(imageSource).uri;
      // 转换成base64
      RNFetchBlob.config({
        fileCache: true,
      })
        .fetch('GET', imageUri)
        .then((resp) => {
          return resp.readFile('base64');
        })
        .then((base64Data) => {
          this.icon = base64Data;
        });
    }
    return ShareSingleton.instance;
  }
  public realToShare() {
    const options: ShareOptions = Platform.select({
      ios: {
        activityItemSources: [
          {
            placeholderItem: {
              type: 'text',
              content: ShareSingleton.icon,
            },
            item: {
              default: {
                type: 'text',
                content: `${SHARE_MESSAGE} ${APP_STORE_URL}`,
              },
            },
            linkMetadata: {
              title: SHARE_MESSAGE,
              icon: ShareSingleton.icon,
            },
          },
        ],
      },
      default: {
        title: APP_NAME,
        subject: APP_NAME,
        message: `${SHARE_MESSAGE} ${APP_STORE_URL}`,
      },
    });
    Share.open(options)
      .then((res) => {
        console.log(res);
      })
      .catch((err) => {
        console.warn(err);
      });
  }

  public async shareToFacebook() {
    const title = APP_NAME;
    const message = SHARE_MESSAGE;
    const shareOptions: ShareSingleOptions = {
      title: title,
      message: message,
      url: APP_STORE_URL,
      social: Social.Facebook,
    };
    try {
      const shareResponse = await Share.shareSingle(shareOptions);
      console.log('start======================');
      console.log('shareResponse', shareResponse);
      console.log('end========================');
    } catch (error) {
      console.log('start======================');
      console.log('error', error);
      console.log('end========================');
    }
  }

  public async shareVideo(file: string) {
    const videoPath = Platform.select({
      ios: file,
      android: 'file:///storage/emulated/0/Download/video.mp4', // Android路径注意使用完整路径
    });
    console.log('start======================');
    console.log('videoPath = ', videoPath);
    console.log('end========================');
    const options = {
      url: videoPath,
      type: 'video/mp4',
      failOnCancel: false, // 可选，取消时不抛错
    };

    try {
      const res = await Share.open(options);
      console.log('Share result:', res);
    } catch (err) {
      console.log('Share error:', err);
    }
  }

  async shareToTwitter() {
    const url = APP_STORE_URL;
    const title = APP_NAME;
    const message = SHARE_MESSAGE;
    const shareOptions: ShareSingleOptions = {
      title: title,
      message: message,
      url: url,
      social: Social.Twitter,
    };
    try {
      const shareResponse = await Share.shareSingle(shareOptions);
      if (shareResponse.message === undefined) {
        Toast.show({
          type: 'lightWeightToast',
          props: {
            _subType: 'error',
            _message: 'Please Install Or Login X',
          },
        });
      }
      console.log('end========================');
    } catch (error) {
      console.log('start======================');
      console.log('error', error);
      console.log('end========================');
    }
  }
}
